package com.tcm.order.service.settlement;


import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.genid.generator.UidGenerator;
import com.tcm.order.entity.OrderDoctorBill;
import com.tcm.order.entity.Order;
import com.tcm.order.entity.OrderStoreBill;
import com.tcm.order.entity.SettlementOrder;
import com.tcm.order.dao.mapper.SettlementOrderMapper;
import com.tcm.order.service.SettlementOrderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@DubboService(version = "0.0.1-alpha", timeout = 6000, retries = 3, delay = -1)
public class SettlementOrderServiceImpl implements SettlementOrderService {

    @Autowired
    private SettlementOrderMapper settlementOrderMapper;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", timeout = 6000, check = false, retries = 3)
    private UidGenerator uidGenerator;

    @Override
    public void create(Order order, OrderStoreBill orderStoreBill, OrderDoctorBill orderDoctorBill, BigDecimal inviterCommission) {

        SettlementOrder settlementOrder = new SettlementOrder();
        settlementOrder.setId(uidGenerator.getUID());
        settlementOrder.setOrderId(order.getId());
        settlementOrder.setDoctorUserId(order.getDoctorId());
        settlementOrder.setStoreId(order.getStoreId());
        settlementOrder.setBillingMoney(order.getBillingMoney());
        settlementOrder.setServiceMoney(order.getServiceMoney());
        settlementOrder.setTotalServiceMoney(NumberUtil.mul(order.getServiceMoney(),order.getQuantity()));
        settlementOrder.setDrugMoney(order.getDrugMoney());
        settlementOrder.setQuantity(order.getQuantity().intValue());
        settlementOrder.setProcessCost(order.getProcessCost());
        settlementOrder.setDecoctingFee(order.getDecoctingFee());
        settlementOrder.setLogisticsFee(order.getLogisticsFee());
        settlementOrder.setDeductPoints(order.getDeductPoints());
        settlementOrder.setDeductPointMoney(order.getDeductPointMoney());
        settlementOrder.setOrderTotals(order.getTotalMoney());
        settlementOrder.setPayAmount(order.getPayAmount());
        settlementOrder.setPayMoney(order.getPayMoney());
        settlementOrder.setDrugsMoney(NumberUtil.mul(order.getDrugMoney(),order.getQuantity()));

        if (ObjectUtil.isNotNull(orderDoctorBill)) {
            settlementOrder.setDoctorBillingCommission(orderDoctorBill.getBillingCommission());
            settlementOrder.setDoctorServiceCommission(orderDoctorBill.getDrugsCommission());
            settlementOrder.setDoctorDrugsCommission(orderDoctorBill.getDrugsCommission());
            settlementOrder.setDoctorTotalCommission(orderDoctorBill.getTotalCommission());
        }
        settlementOrder.setStoreDrugsCommission(orderStoreBill.getDrugsCommission());
        settlementOrder.setInviterCommission(inviterCommission);
        settlementOrder.setStoreTotalCommission(orderStoreBill.getTotalMoney());
        settlementOrder.setCreateTime(LocalDateTime.now());
        settlementOrderMapper.insert(settlementOrder);
    }

    @Override
    public List<SettlementOrder> selectByEndTime(LocalDateTime endTime) {
        return settlementOrderMapper.selectByEndTime(endTime);
    }

    @Override
    public void updateByEndTime(LocalDateTime endTime) {
        settlementOrderMapper.updateByEndTime(endTime);
    }
}
